/*
 * WhatsThisManagerDelegate.cpp  0.4.0 / Nov 17, 2010
 *
 * Copyright (c) 2010 - 2011, cute4j.com and contributors.
 *
 * This software is licensed under the LGPL version 2.1 license.
 * A copy of this license is included in the file LICENSE-LGPL.txt.
 */
#include "WhatsThisManagerDelegate.h"

#include "ComponentDelegate.h"

// WhatsThisManagerDelegate :: Static Native Calls. ----------------------------

/*!
 * \brief Enables/disables \c <what's this> help.
 *
 * \param env the JNIEnv pointer
 * \param cls the java class
 * \param enabled \c true if enabled
 */
void WhatsThisManagerDelegate::setEnabled(JNIEnv *env, jclass cls,
                                          jboolean enabled)
{
  Q_UNUSED(env);
  Q_UNUSED(cls);
  sm_enabled = enabled;
}

/*!
 * \brief Displays \c <what's this> help for the specified component.
 *
 * \param env the JNIEnv pointer
 * \param cls the WhatsThisManager class
 * \param text the  what's this text
 * \param x the x-coordinate of the \c <what's this> help
 * \param y the y-coordinate of the \c <what's this> help
 * \param componentDelegate the component delegate pointer
 */
void WhatsThisManagerDelegate::showText(JNIEnv *env, jclass cls, jstring text,
                                        jint x, jint y, jlong componentDelegate)
{
  Q_ASSERT(env);
  SWING_STATIC_REENTRANT(WhatsThisManagerDelegate, showText, SWING_ENV_ARG,
                         JCLASS_ARG(cls), JSTRING_ARG(text), JINT_ARG(x),
                         JINT_ARG(y), JLONG_ARG(componentDelegate))
  if (sm_enabled) {
    if (componentDelegate) {
      QWidget *widget = COMPONENT_CAST(componentDelegate)->qWidget();
      QWhatsThis::showText(widget->mapToGlobal(QPoint(x, y)),
                           SwingString(text), widget);
    } else {
      QWhatsThis::showText(QPoint(x, y), SwingString(text));
    }
  }
}

/*!
 * \brief Hides \c <what's this> help.
 *
 * \param env the JNIEnv pointer
 * \param cls the WhatsThisManager class
 */
void WhatsThisManagerDelegate::hideText(JNIEnv *env, jclass cls)
{
  Q_ASSERT(env);
  SWING_STATIC_REENTRANT(WhatsThisManagerDelegate, hideText, SWING_ENV_ARG,
                         JCLASS_ARG(cls))
  QWhatsThis::hideText();
}

/*!
 * \brief Returns \c true if \c <what's this> help is active.
 *
 * \param env the JNIEnv pointer
 * \param cls the WhatsThisManager class
 * 
 * \return 
 */
jboolean WhatsThisManagerDelegate::isInWhatsThisMode(JNIEnv *env, jclass cls)
{
  Q_ASSERT(env);
  SWING_STATIC_REENTRANT_RETURN(WhatsThisManagerDelegate, isInWhatsThisMode,
                                jboolean, SWING_ENV_ARG, JCLASS_ARG(cls))
  return QWhatsThis::inWhatsThisMode();
}

/*!
 * \brief Activates \c <what's this> help. 
 *
 * \param env the JNIEnv pointer
 * \param cls the WhatsThisManager class
 */
void WhatsThisManagerDelegate::enterWhatsThisMode(JNIEnv *env, jclass cls)
{
  if (sm_enabled) {
    Q_ASSERT(env);
    SWING_STATIC_REENTRANT(WhatsThisManagerDelegate, enterWhatsThisMode,
                           SWING_ENV_ARG, JCLASS_ARG(cls))
    QWhatsThis::enterWhatsThisMode();
  }
}

/*!
 * \brief Deactivates \c <what's this> help. 
 *
 * \param env the JNIEnv pointer
 * \param cls the java class
 */
void WhatsThisManagerDelegate::exitWhatsThisMode(JNIEnv *env, jclass cls)
{
  if (sm_enabled) {
    Q_ASSERT(env);
    SWING_STATIC_REENTRANT(WhatsThisManagerDelegate, exitWhatsThisMode,
                           SWING_ENV_ARG, JCLASS_ARG(cls))
    QWhatsThis::leaveWhatsThisMode();
  }
}

// WhatsThisManagerDelegate :: Private Static Data Member. ---------------------

bool WhatsThisManagerDelegate::sm_enabled = true;
